package com.example.team6.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.example.team6.entity.Student;
import com.example.team6.entity.Teacher;
import com.example.team6.entity.resultt;
//import com.example.team6.service.AdminService;
import com.example.team6.mapper.StudentMapper;
import com.example.team6.service.StudentService;
//import com.example.team6.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wwj
 * @since 2020-06-28
 */
@RestController
@CrossOrigin
//@RequestMapping("/team6/student")
public class StudentController {
    //    @Autowired
//    TeacherService teacherService;
    @Autowired
    StudentService studentService;
    @Autowired
    StudentMapper studentMapper;
   // @Autowired
  //  AdminService adminService;
    // @Autowired
    // MessageMapper messageMapper;

    @PostMapping(value = "api/stulogin")
    @ResponseBody
    public resultt stulogin(@RequestBody Student requestUser) {
        resultt resultt = new resultt();

        QueryWrapper<Student> studentQueryWrapper = new QueryWrapper<>();
        studentQueryWrapper.eq("stu_account", requestUser.getStuAccount());
        // System.out.println(requestUser.getAdmAccount());
        studentQueryWrapper.eq("stu_pass", requestUser.getStuPass());

        Student student = studentService.getOne(studentQueryWrapper);
        //System.out.println(admin);
        if (null == student) {
            String massage = "账号密码错误";
            return new resultt(400);
        } else {
            System.out.println("test");
            Map<String, Object> map = new HashMap<String, Object>();
            resultt.setCode(200);
            map.put("id", student.getId());
            map.put("stuName", student.getStuName());
            resultt.setData(map);
        }
        return resultt;

    }

    @PostMapping(value = "api/sturegister")
    @ResponseBody
    public resultt sturegister (@RequestBody Student requestUser){

        resultt resultt = new resultt();

        Student student = new Student();
        student.setStuAccount(requestUser.getStuAccount());
        student.setStuPass(requestUser.getStuPass());
        student.setStuName(requestUser.getStuName());
        Integer result = studentMapper.insert(student);

        if (null == result) {
            String massage = "账号密码错误";
            return new resultt(400);
        } else {
            System.out.println("test");
            Map<String, Object> map = new HashMap<String, Object>();
            resultt.setCode(200);
            map.put("adminid", student.getId());
            resultt.setData(map);
        }
        return resultt;
    }

        @PostMapping(value = "api/modifyinfo")
        @ResponseBody
        public void modifyinfo (@RequestBody Student requestUser){

            Student student = new Student();
            student.setId(requestUser.getId());
            student.setEmail(requestUser.getEmail());
            student.setTel(requestUser.getTel());
            student.setHabit(requestUser.getHabit());
            student.setStuName(requestUser.getStuName());
            Integer result = studentMapper.updateById(student);

        }

    @PostMapping(value = "api/stupersion")
    @ResponseBody
    public List<Student> stupersion (@RequestBody Student requestUser) {

        System.out.println(requestUser.getId());
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("id", requestUser.getId());
        List<Student> studentList = studentMapper.selectByMap(map);

        return studentList;
    }

    }